<template>
  <div class="el_main_con">
    <Breadcrumb></Breadcrumb>
    <el-card>
      <el-button type="primary" @click="showAddDialogTrue()">添加分类</el-button>
      <zk-table ref="cateForm" :data="cateList" :columns="cateFormColumns" :selection-type='false' :expand-type='false' show-index index-text="#" border>
        <template slot="catDeleted" slot-scope="scope">
          <i class="el-icon-success" style="color: lightgreen;font-size: 18px;" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="danger" v-if="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openShowEdit(scope.row)">编辑</el-button>
          <el-button class="deleBtn" type="danger" size="mini" icon="el-icon-delete" @click="deleteCate(scope.row)">删除</el-button>
        </template>
      </zk-table>
      <div class="paginationBox">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="CategoriesInfo.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="CategoriesInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="添加分类" :visible.sync="showAddDialog" width="30%" @close="cloaseAddDialog()">
      <el-form :model="addCateForm" :rules="addCateFormrules" ref="addCateForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类层级" prop="cat_level">
          <el-cascader v-model="addCateForm.cat_level" :options="catLevelOptions" :props="catecadeProps" clearable @change="parentCateChanged()"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cloaseAddDialog()">取 消</el-button>
        <el-button type="primary" @click="addCate()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑分类" :visible.sync="showEditDialog" width="30%">
      <el-form :model="editCateForm" :rules="editCateForm" ref="editCateForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cloaseEditDialog()">取 消</el-button>
        <el-button type="primary" @click="editCate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from 'components/breadcrumb/breadcrumb'
import { Categories } from '@/api/apiConfig'
export default {
  components: {
    Breadcrumb
  },
  data () {
    return {
      CategoriesInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      total: 0,
      cateFormColumns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'catDeleted'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
          width: '195px'
        }
      ],
      showAddDialog: false,
      addCateForm: {
        cat_pid: '',
        cat_name: '',
        cat_level: []
      },
      addCateFormrules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
        cat_level: [
          { required: true, message: '请输入分类层级', trigger: 'blur' }
        ]
      },
      catLevelOptions: [],
      catecadeProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      postCategories: {
        // 分类父 ID不能为空，如果要添加1级分类，则父分类Id应该设置为  `0`
        cat_pid: 0,
        // 分类名称不能为空
        cat_name: '',
        // 分类层级不能为空，`0`表示一级分类；`1`表示二级分类；`2`表示三级分类
        cat_level: 0
      },
      showEditDialog: false,
      editCateForm: {
        id: '',
        cat_name: ''
      }
    }
  },
  mounted () {
    this.getCategoriesList()
  },
  methods: {
    getCategoriesList () {
      Categories.getCategories(this.CategoriesInfo).then(res => {
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.cateList = res.data.result
        this.total = res.data.total
      })
    },
    handleSizeChange (newSize) {
      this.CategoriesInfo.pagesize = newSize
      this.getCategoriesList(this.CategoriesInfo)
    },
    handleCurrentChange (newPage) {
      this.CategoriesInfo.pagenum = newPage
      this.getCategoriesList(this.CategoriesInfo)
    },
    cloaseAddDialog () {
      this.showAddDialog = false
      this.$refs.addCateForm.resetFields()
      this.postCategories = {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      }
    },
    showAddDialogTrue () {
      this.showAddDialog = true
      this.getParentCateList()
    },
    getParentCateList () {
      const params = {
        type: 2
      }
      Categories.getCategories(params).then(res => {
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.catLevelOptions = res.data
      })
    },
    parentCateChanged () {
      if (this.addCateForm.cat_level.length > 0) {
        this.postCategories.cat_pid = this.addCateForm.cat_level[this.addCateForm.cat_level.length - 1]
        this.postCategories.cat_level = this.addCateForm.cat_level.length
        return false
      } else {
        this.postCategories.cat_pid = 0
        this.postCategories.cat_level = 0
      }
    },
    addCate () {
      this.postCategories.cat_name = this.addCateForm.cat_name
      Categories.addCategories(this.postCategories).then(res => {
        if (res.meta.status === 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.cloaseAddDialog()
        this.getCategoriesList()
      })
    },
    openShowEdit (row) {
      this.showEditDialog = true
      this.editCateForm = {
        id: row.cat_id,
        cat_name: row.cat_name
      }
    },
    deleteCate (row) {
      this.$confirm('确定删除该分类?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = `categories/${row.cat_id}`
        Categories.deleteCategories(url).then(res => {
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getCategoriesList()
        })
      }).catch(() => { })
    },
    cloaseEditDialog () {
      this.showEditDialog = false
      this.$refs.editCateForm.resetFields()
    },
    editCate () {
      const url = `categories/${this.editCateForm.id}`
      const params = {
        cat_name: this.editCateForm.cat_name
      }
      Categories.editCate(url, params).then(res => {
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.$refs.editCateForm.resetFields()
        this.showEditDialog = false
        this.getCategoriesList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
